package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//游记资源控制器
@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 编辑游记
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Travel getById(@PathVariable Long id){
        return travelService.getById(id);
    }

    /**
     * 修改游记    travels/1/    put
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public JSONResult update(Travel travel){
        JSONResult result = new JSONResult();

        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

    /**
     * 查询所有游记
     */
    @RequestMapping(method = RequestMethod.GET)
    public PageInfo query(TravelQueryObject qo){
        //公开
        qo.setIsPublic(true);
        //已发布
        qo.setState(Travel.STATE_RELEASE);
        //发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.querForList(qo);
    }



    /**
     * 游记推荐
     */
    @RequestMapping(value = "/commends",method = RequestMethod.GET)
    public PageInfo queryCommend(TravelCommendQueryObject qo){
        //按推荐时间安排倒序
        qo.setOrderBy("tc.schedule desc");

        return travelCommendService.queryForApp(qo);
    }
}
